package com.tanxi521.springboot.controller;

import com.tanxi521.springboot.bean.OfficialMessageBean;
import com.tanxi521.springboot.pojo.*;
import com.tanxi521.springboot.service.OfficialMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 游戏顶部滚动播报消息
 */
@RestController
@RequestMapping("message")
@Slf4j
@CrossOrigin
public class OfficialMessageController {

    @Autowired
    private OfficialMessageService officialMessageService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("list")
    @RestPoint
    public APIResult getMessageList(Integer pageSize, Integer pageIndex, String message) {

        OfficialMessage officialMessage = new OfficialMessage();
        officialMessage.setTnVexMessage(message);
        List<OfficialMessage> officialMessageList = officialMessageService.queryMessageList(pageSize, pageIndex, officialMessage);
        int count = officialMessageService.queryMessageCount(officialMessage);
        OfficialMessageBean officialMessageBean = new OfficialMessageBean();
        officialMessageBean.setOfficialMessageList(officialMessageList);
        officialMessageBean.setCount(count);
        officialMessageBean.setPageCount(count / pageSize + 1);
        return new APIResult(officialMessageBean, APIResult.SUCCESS, "查询消息列表成功");
    }

    @PostMapping("add")
    @RestPoint
    public APIResult addMessage(@RequestBody OfficialMessage officialMessage, HttpServletRequest request) {
        String token = request.getHeader("X-Token");
        OaUser oa_user = (OaUser)redisTemplate.opsForValue().get(token);
        if (oa_user.getUserRole() != 1) {
            log.warn(oa_user.getUserName() + " 尝试无权限修改");
            return new APIResult(APIResult.FAIL, "你莫得权限");
        }
        int count = officialMessageService.insertOfficialMessage(officialMessage);
        if (count != 0) {
            log.info(oa_user.getUserName() + " 添加轮播消息 " + officialMessage.getTnVexMessage());
            return new APIResult(APIResult.SUCCESS, "添加轮播消息成功");
        } else {
            return new APIResult(APIResult.FAIL, "添加轮播消息失败，请重试");
        }
    }

    @DeleteMapping("delete")
    @RestPoint
    public APIResult deleteMessage(@RequestBody DeleteIds deleteIds, HttpServletRequest request) {
        String token = request.getHeader("X-Token");
        OaUser oa_user = (OaUser)redisTemplate.opsForValue().get(token);
        if (oa_user.getUserRole() != 1) {
            log.warn(oa_user.getUserName() + " 尝试无权限修改");
            return new APIResult(APIResult.FAIL, "你莫得权限");
        }
        int count = officialMessageService.deleteMessageById(deleteIds.getIds());
        if (count != 0) {
            log.warn(oa_user.getUserName() + " 删除了一条轮播消息");
            return new APIResult(APIResult.SUCCESS, "删除轮播消息成功");
        } else {
            return new APIResult(APIResult.FAIL, "删除轮播消息失败，请重试");
        }
    }

}
